package cn.dankal.flyaudio.personal;

import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;

import api.UserServiceFactory;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.UserInfoBean;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.business.pojo.ReasonBean;
import cn.dankal.flyaudio.R;

public class SettingNickNameActivity extends BaseActivity {

    private EditText nickNameView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting_nickname;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("设置昵称");
        nickNameView = findViewById(R.id.et_nickname);
        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nickName = nickNameView.getText().toString();
                if (StringUtils.isEmpty(nickName)) {
                    ToastUtils.showShort("请输入昵称");
                    return;
                }
                UserServiceFactory.changeName(nickName)
                        .subscribe(new AbstractDialogSubscriber<ReasonBean>(SettingNickNameActivity.this) {
                            @Override
                            public void onNext(ReasonBean reasonBean) {
                                UserInfoBean userInfoBean = DKUserManager.getUserInfo();
                                userInfoBean.setName(nickName);
                                DKUserManager.updateUserInfo(userInfoBean);
                                LiveEventBus.get("update_user_info", String.class).post("");

                                finish();
                            }
                        });
            }
        });
    }

}
